package cn.com.cmbc.service;

import cn.com.cmbc.config.HBaseConfig;
import cn.com.cmbc.dao.HBaseDAO;
import cn.com.cmbc.dto.HbaseDTO;
import cn.com.cmbc.utils.HTTPUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

@Service
public class HbaseService {
    private static final Logger LOG = LoggerFactory.getLogger(HbaseService.class);

    private final Connection connection;

    public HbaseService() throws IOException {
        this.connection = new HBaseConfig().hbaseConnection();
    }

    public boolean putListData(String tableName, String family,  List<HBaseDAO> hBaseDAOS) {
        for (HBaseDAO hBaseDAO : hBaseDAOS) {
            try {
                putData(tableName, hBaseDAO.getRowkeyId(),family,"name", hBaseDAO.getName());
                putData(tableName, hBaseDAO.getRowkeyId(),family,"lastModifiedTime",String.valueOf(hBaseDAO.getLastModifiedTime()));
                putData(tableName, hBaseDAO.getRowkeyId(),family,"type", hBaseDAO.getType());
                putData(tableName, hBaseDAO.getRowkeyId(),family,"suffix", hBaseDAO.getSuffix());
                putData(tableName, hBaseDAO.getRowkeyId(),family,"size",String.valueOf(hBaseDAO.getSize()));
                putData(tableName, hBaseDAO.getRowkeyId(),family,"absolutePath", hBaseDAO.getAbsolutePath());
            } catch (IOException e) {
                LOG.error("Failed to put data for row key: " + hBaseDAO.getRowkeyId() + ", error: " + e.getMessage());
                return false;
            }
        }
        return true;
    }


    public void putData(String tableName, String rowKey, String family, String qualifier, String value) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(rowKey.getBytes());
        put.addColumn(family.getBytes(), qualifier.getBytes(), value.getBytes());
        table.put(put);
        table.close();
    }
    public String getData(String tableName, String rowKey, String family, String qualifier) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(rowKey.getBytes());
        get.addColumn(family.getBytes(), qualifier.getBytes());
        Result result = table.get(get);
        byte[] value = result.getValue(family.getBytes(), qualifier.getBytes());
        table.close();
        return value != null ? new String(value) : null;
    }

    public void getFileJSON(String url) {
        String json = HTTPUtil.get(url);
    }


    public List<HbaseDTO> json2DTO(String json) {
        Gson gson = new Gson();
        Type fileDTOListType = new TypeToken<List<HbaseDTO>>() {}.getType();

        List<HbaseDTO> hbaseDTOList = gson.fromJson(json, fileDTOListType);

        return hbaseDTOList;
    }


}
